package com.fifth.apartment.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fifth.apartment.entity.Contract;
import com.fifth.apartment.mapper.ContractMapper;
import com.fifth.apartment.service.ContractService;
import com.fifth.apartment.utils.OssUtil;
import com.fifth.apartment.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/4
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Autowired
    private  ContractMapper mapper;
    @Transactional(readOnly = true)
    @Override
    public Page<Contract> findpage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        Page<Contract> page1 = new Page<>(currentPage,pageSize);

        LambdaQueryWrapper<Contract> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasLength(queryString), Contract::getCName,queryString)
                .or()
                .like(StringUtils.hasLength(queryString),Contract::getSignedPeople,queryString);
        Page<Contract> page2 = this.baseMapper.selectPage(page1,lambdaQueryWrapper);

        return page2;
    }

    @Transactional(readOnly = true)
    @Override
    public Contract getContract(Integer id) {
        Contract contract = mapper.getContract(id);
        return contract;
    }

    @Transactional
    @Override
    public Boolean deleteById(Integer id) {
        int flag = mapper.deleteById(id);
        return flag>0?true:false;
    }

    @Override
    public Boolean insertContract(Contract contract) {
        int insert = mapper.insert(contract);
        return insert>0?true:false;
    }

    @Override
    public String uploadByAliyun(MultipartFile imgFile) {
        if(imgFile!=null){
            // Endpoint以上海为例，其它Region请按实际情况填写。
            String endpoint = OssUtil.EDNPOINT;
            String accessKeyId = OssUtil.ACCESS_KEY_ID;
            String accessKeySecret = OssUtil.SECRECT;
            String bucketName = OssUtil.BUCKET;

            try {
                //创建OSSClient实例
                OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

                //TODO 获取上传文件的流对象 inputStream
                InputStream inputStream = imgFile.getInputStream();

                //TODO 处理上传文件名称 fileName
                String oldName = imgFile.getOriginalFilename();
                if(StringUtils.hasLength(oldName)){
                    String newName = UUID.randomUUID()+oldName;
                    String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                    String fileName = date +"/"+ newName;  //  2021/05/06/0a4954938c5e488886924a01eca95ebd1.png

                    //调用方法实现上传
                    ossClient.putObject(bucketName, fileName, inputStream);
                    //关闭OSSClient
                    ossClient.shutdown();

                    //TODO 返回上传之后文件路径 url
                    //格式例如：https://igeek-health.oss-cn-shanghai.aliyuncs.com/2021/05/06/0a4954938c5e488886924a01eca95ebd1.png
                    String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
                    return url;
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Boolean uploadById(String url, Integer id) {
       Boolean flag =  mapper.updateImgById(id,url);
        return flag;
    }

    @Override
    public String  viewById(Integer id) {
        String url = mapper.selectUrlById(id);
        return url;
    }

}
